﻿''' <summary>
''' A component that will generate icons from files or filetypes.
''' </summary>
''' <remarks></remarks>
Public Class FileIconListCreator

    Public IconSize As FileIcon.enmIconSize = FileIcon.enmIconSize.Large

    

    Private _Generating As Boolean
    ''' <summary>
    ''' Gets whether the component is generating icons at the current time.
    ''' </summary>
    ''' <value>Boolean</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IsGenerating() As Boolean
        Get
            Return _Generating
        End Get
        Private Set(ByVal value As Boolean)
            _Generating = value
        End Set
    End Property


    Private _files As List(Of String)
    ''' <summary>
    ''' Gets or sets the list of files or extensions to generate icons from.
    ''' </summary>
    ''' <value>List of files.</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Files() As List(Of String)
        Get
            Return _files
        End Get
        Set(ByVal value As List(Of String))
            If Me.IsGenerating Then Throw New System.InvalidOperationException("Cannot change the list of files while a list of icons is being generated.")
            _files = value
        End Set
    End Property
    ''' <summary>
    ''' Call this to start generating.  You can only call this when the component is not generating.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Generate()
        If Me.IsGenerating Then Throw New System.InvalidOperationException("Cannot generate icons while a list of icons is being generated.")
        Dim th As New Threading.Thread(AddressOf GenerateIcons)
        th.IsBackground = True
        th.SetApartmentState(Threading.ApartmentState.STA)
        th.Start()
    End Sub
    ''' <summary>
    ''' Does the work of retreving the icons and descriptions.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GenerateIcons()
        Dim results As New List(Of FileInfo)
        For Each ext As String In Me.Files
            Dim fdesc As String = ""
            Dim ic As Bitmap = FileIcon.GetIconAsImageFromExtension(ext, Me.IconSize, fdesc)
            ic.MakeTransparent()
            results.Add(New FileInfo(ext, fdesc, ic))
        Next

        RaiseEvent GenerateIconsComplete(Me, New GeneratiionDoneEventArgs(results))
        Me.IsGenerating = False
    End Sub
    ''' <summary>
    ''' Notifies user of component that the genration is complete.  
    ''' </summary>
    ''' <param name="Sender">FileIconListCreator that raised the event.</param>
    ''' <param name="e">The eventargs that holds the list of fileinfos.</param>
    ''' <remarks>Will run in another thread.  
    ''' Make sure you check with 
    ''' <seealso cref="System.Windows.Forms.Control.InvokeRequired">InvokeRequired</seealso> and use invoke to run form manipulation code on the form thread.   </remarks>
    Public Event GenerateIconsComplete(ByVal Sender As Object, ByVal e As GeneratiionDoneEventArgs)
    ''' <summary>
    ''' Holds a list of fileinfo objects.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GeneratiionDoneEventArgs
        Inherits EventArgs
        Private _Infos As List(Of FileInfo)

        Public Sub New(ByVal Infos As List(Of FileInfo))
            MyBase.New()
            _Infos = Infos

        End Sub
        ''' <summary>
        ''' Gets the list of FileInfo objects.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Infos() As List(Of FileInfo)
            Get
                Return _Infos
            End Get
        End Property

    End Class
End Class

